#!/bin/python
# Copyright (C) 2010 Iain Surgey.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
#
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.


import sys
import os.path
import struct
import zlib
import random

def convtga(inf, outf):
	
	tgabin = inf.read()
	inf.close()
	
	header = struct.unpack("<BBBhhBhhHHBB",tgabin[:18])
	
	id_length = header[0]
	width = header[8]
	height = header[9]
	pixel_size = header[10]
	
	if header[2] != 2:
		print "Error : Unsupported image type : %" % header[2]
		sys.exit()
	
	rows = []
	
	for i in range(0,height):
		rows.append("")
	
	out = struct.pack("<HH", width, height)
	tgabin = tgabin[18:]
	
	bptr = 0
	
	row = height;
	while row > 0:
		row = row - 1
		rowstr = ""
		column = 0;
		while column < width:
			column = column + 1
			if pixel_size == 24:
				b, g, r = struct.unpack("<BBB", tgabin[bptr:bptr+3])
				#if (b&7) > 3 and b < 0xF8:
				#	b = b + 7
				#if (g&3) > 2 and g < 0xFC:
				#	g = g + 3
				#if (r&7) > 3 and r < 0xF8:
				#	r = r + 7
				rowstr = rowstr + struct.pack("<H", ((g&0xFC)<<8) | ((r&0xF8)<<2) | ((b&0xF8)>>3) )
				bptr = bptr + 3
			elif pixel_size == 32:
				b, g, r, a = struct.unpack("<BBBB", tgabin[bptr:bptr+4])
				#if (b&7) > 3 and b < 0xF8:
				#	b = b + 7
				#if (g&3) > 2 and g < 0xFC:
				#	g = g + 3
				#if (r&7) > 3 and r < 0xF8:
				#	r = r + 7
				rowstr = rowstr + struct.pack("<H", ((g&0xFC)<<8) | ((r&0xF8)<<2) | ((b&0xF8)>>3) )
				bptr = bptr + 4
			else:
				print "Error : Unsupported pixel size : %" % pixel_size
				sys.exit()
		rows[row] = rowstr
	
	for i in range(0,height):
		out = out + rows[i]
	
	out = zlib.compress(out,9)
	
	outf.write(out)
	outf.close()
	
	print "Done!"

def main():
	
	if len(sys.argv) < 2:
		print "Error : no input file \nUsage : python hxc.py input output"
		sys.exit()
	
	if len(sys.argv) < 3:
		print "Error : no output file \nUsage : python hxc.py input output"
		sys.exit()
	
	infile = sys.argv[1]
	outfile = sys.argv[2]
	
	
	if not os.path.exists(infile):
		print "File Error : File not found. [%s]" % infile
		sys.exit()
	
	try:
		file = open(infile, "r")
	except:
		print "File Error : Cannot open file for read. [%s]" % infile
		sys.exit()
	
	print "Opened file : %s" % infile
	
	try:
		of = open(outfile,"w")
	except:
		print "File Error : Cannot open file for write. [%s]" % self.cf.name
		sys.exit()
	
	print "Writing to file : %s" % outfile
	
	convtga(file,of)
	
	

if __name__ == "__main__":
	main()


	
	
	
	
